#!/usr/bin/python3
# -*- coding: utf-8 -*-

exec(open("import_qt.py").read())
import traceback


class RingBuffer(QIODevice):
  def __init__(self, size = 1024, par = None):
    super(RingBuffer, self).__init__(par)
    self.open(self.ReadWrite)
    self.buf = bytes()
    self.maxSize = size

  def writeData(self, data):
    #print("write", len(data))
    self.buf += data
    if (len(self.buf)>2*self.maxSize):
      self.buf = self.tail(self.maxSize)
    return len(data)

  def readData(self, size):
    if(size>self.maxSize):
      size = self.maxSize
    return self.tail(size)

  def read(self, size):
    return self.readData(size)

  def tail(self, size):
    b = len(self.buf) - size
    if(b>0):
      return self.buf[b:len(self.buf)]
    else:
      return self.buf

  def dump(self):
    print("->",self.buf)
    


def main():
  buf = RingBuffer(13)
  buf.dump()
  buf.write('ala_ma_kota')
  buf.dump()
  print(buf.read(3))
  buf.dump()
  buf.write(' a kot ma ale')
  buf.dump()
  print(buf.read(120))
  buf.dump()
  buf.write(' a Pan kotek byl chory')
  buf.dump()
  



if __name__ == '__main__':
    main()
